package com.aidong.dynamic.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.dynamic.biz.TrnDynamicCommentBiz;
import com.aidong.dynamic.biz.TrnDynamicContentBiz;
import com.aidong.dynamic.biz.TrnPraiseBiz;
import com.aidong.dynamic.entity.TrnDynamicContent;
import com.aidong.login.biz.MemberLocationReportBiz;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 运动圈动态action
 * @author java
 * @version 1.0    
 *  2015.6.2
 */
@ParentPackage("json")
@Namespace("/")
public class TrnDynamicContentAction implements ModelDriven<TrnDynamicContent> {
	
	private TrnDynamicContent trnDynamicContent; // 运动圈entity

	@Override
	public TrnDynamicContent getModel() {
		if(trnDynamicContent == null){
			trnDynamicContent = new TrnDynamicContent();
		}
		return trnDynamicContent;
	}
	
	private TrnDynamicContentBiz trnDynamicContentBiz; // 运动圈service接口
	
	private TrnDynamicCommentBiz trnDynamicCommentBiz; // 评论service
	
	private TrnPraiseBiz trnPraiseBiz; // 点赞service
	
	private MstCampaignBiz mstCampaignBiz;
	
	//private MemberLocationReportBiz locationReportBiz;
	
	/*@Autowired
	public void setLocationReportBiz(MemberLocationReportBiz locationReportBiz) {
		this.locationReportBiz = locationReportBiz;
	}*/

	/**
	 * 运动圈service接口
	 * @param trnDynamicContentBiz
	 */
	@Autowired
	public void setTrnDynamicContentBiz(TrnDynamicContentBiz trnDynamicContentBiz) {
		this.trnDynamicContentBiz = trnDynamicContentBiz;
	}
	
	/**
	 * 评论service
	 * @param trnDynamicCommentBiz
	 */
	@Autowired
	public void setTrnDynamicCommentBiz(TrnDynamicCommentBiz trnDynamicCommentBiz) {
		this.trnDynamicCommentBiz = trnDynamicCommentBiz;
	}

	/**
	 * 点赞service
	 * @param trnPraiseBiz
	 */
	@Autowired
	public void setTrnPraiseBiz(TrnPraiseBiz trnPraiseBiz) {
		this.trnPraiseBiz = trnPraiseBiz;
	}
	
	/**
	 * 
	 * @param mstCampaignBiz
	 */
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}

	private Map<String, Object> map; // 返回的json数据类型
	
	private int pageCurrent; // 当前页  工具属性  用来分页
	
	/**
	 * 当前页  工具属性  用来分页
	 * @param pageCurrent
	 */
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}
	
	/**
	 * 返回的json数据类型
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}

	
	//2016-03-14 运动圈举报
	private String addReason;
	public void setAddReason(String addReason) {
		this.addReason = addReason;
	}

	private int  ids;
	
	public void setIds(int ids) {
		this.ids = ids;
	}

	/**
	 * 加载运动圈动态
	 * @return
	 */
	@Action(value="gettrndynamic",results={@Result(name="succ",type="json",params={"root","map"})})
	public String findExes(){
		/*try {
			int idongId = trnDynamicContent.getIdongId();
			
			String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String hour = new SimpleDateFormat("HH").format(new Date());
			
			// 记录用户活跃度
			if(idongId > 0){
				// 是否在当天某时已有记录
				Map<String, Object> memberMap = new HashMap<String, Object>();
				memberMap.put("date", date);
				memberMap.put("menuType", 2);
				memberMap.put("idongId", idongId);
				Map<String, Object> userCountMap = locationReportBiz.getFunctionUseCount(memberMap);
				if(userCountMap != null){
					// 修改用户活跃数 
					int userCount = Integer.parseInt(userCountMap.get("count") + "");
					userCountMap.put("liveCount", userCount + 1);
					locationReportBiz.editFunctionUseCount(userCountMap);
				}else{
					// 记录用户活跃数
					memberMap.put("liveCount", 1);
					memberMap.put("liveType", 2);
					locationReportBiz.addFunctionUseLiveness(memberMap);
				}
			}
			// 是否在当天某时已有记录
			Map<String, Object> useMap = new HashMap<String, Object>();
			useMap.put("date", date);
			useMap.put("menuType", 2);
			useMap.put("hour", hour);
			Map<String, Object> countMap = locationReportBiz.getFunctionUseCount(useMap);
			if(countMap != null){
				// 修改使用活跃数
				int recordCount = Integer.parseInt(countMap.get("count") + "");
				useMap.put("liveCount", recordCount + 1);
				locationReportBiz.editFunctionUseCount(useMap);
			}else{
				// 记录使用活跃度
				useMap.put("liveCount", 1);
				useMap.put("liveType", 1);
				locationReportBiz.addFunctionUseLiveness(useMap);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
		map = new HashMap<String, Object>();
		try {
			int idongId = trnDynamicContent.getIdongId(); // 爱动号
			String flag = trnDynamicContent.getFlag(); // 标记位   1、最新：newest(默认)  2、最热：hottest   3、关注：focus
			// 分页
			Map<String, Object> pageMap = PageUtil.page(10, pageCurrent);
			List<Map<String, Object>> dynamicMap = new ArrayList<Map<String, Object>>();
			
			if("newest".equals(flag)){ // 关注的好友的动态
				// 按分页参数查找运动圈动态
				dynamicMap = trnDynamicContentBiz.getTrnDynamic(pageMap);
			}
			
			if("focus".equals(flag) && idongId > 0){ // 关注的好友的动态
				pageMap.put("idongId", idongId);
				pageMap.put("flag", 3);
				// 按分页参数查找运动圈动态
				dynamicMap = trnDynamicContentBiz.getTrnDynamic(pageMap);
			}
			
			if("hottest".equals(flag)){ // 点赞数做多的动态
				dynamicMap = trnDynamicContentBiz.getDynamicHot(pageMap);
			}
			
			for (Map<String, Object> dynamic : dynamicMap) {
				
				dynamic.put("headPic", ReadPropertiesUtil.getIMGREAD(dynamic.get("headPic")));
				dynamic.put("picture", ReadPropertiesUtil.getIMGREAD(dynamic.get("picture")));

				int dynamicId = (Integer) dynamic.get("dynamicId"); // 动态id
				if ("newest".equals(flag)) {
					// 评论数
					Map<String, Object> commMap = new HashMap<String, Object>();
					commMap.put("dynamicId", dynamicId);
					int commentCount = trnDynamicCommentBiz.getCommentCount(commMap);
					dynamic.put("commentCount", commentCount);
					// 点赞数
					Map<String, Object> praiseMap = new HashMap<String, Object>();
					praiseMap.put("dynamicId", dynamicId);
					int praiseCount = trnPraiseBiz.getPraiseCount(praiseMap);
					dynamic.put("praiseCount", praiseCount);
					
					if(idongId > 0){
						// 查看当前登录用户是否对此条动态已点赞
						Map<String, Object> countMap = new HashMap<String, Object>();
						countMap.put("dynamicId", dynamicId);
						countMap.put("idongId", idongId);
						countMap.put("status", 1);
						int count = trnPraiseBiz.getCountPraise(countMap);
						if(count > 0){ // 已点赞
							dynamic.put("status", 1);
						}else{// 未点赞
							dynamic.put("status", 2);
						}
					}else{
						dynamic.put("status", 2);
					}
				}
				
				
				//add 2016-03-02 查询已关注用户的前三条运动圈
				if ("focus".equals(flag)) {
					List<Map<String, Object>> dynamicThreeMap = new ArrayList<Map<String, Object>>();
					Map<String, Object> dynamicThreeParaMap = new HashMap<String, Object>();
					dynamicThreeParaMap.put("idongId", dynamic.get("idongId"));
					dynamicThreeMap = trnDynamicContentBiz.getPersonDynamicTopThree(dynamicThreeParaMap);
					//循环加前缀
					for (Map<String, Object> dynamicHere : dynamicThreeMap) {
						dynamicHere.put("picture", ReadPropertiesUtil.getIMGREAD(dynamicHere.get("picture")));
					}
					dynamic.put("circles", dynamicThreeMap);
				}
				
				
				// 关注状态
				/*Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId); // 关注人
				paraMap.put("tionIdongId", dynamic.get("idongId")); // 被关注人
				int attionCount = mstCampaignBiz.getIfAttention(paraMap);
				if(idongId > 0){
					if(attionCount > 0){ // 已关注
						dynamic.put("attionStatus", 1);
					}else{ // 关注
						dynamic.put("attionStatus", 2);
					}
				}else{ // 关注
					dynamic.put("attionStatus", 2);
				}*/
			}
			
			map.put("code", 1);
			map.put("result", dynamicMap);
			map.put("pageCurrent", pageMap.get("pageCurrent"));
			return "succ";
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "系统异常");
			e.printStackTrace();
			return "succ";
		}
	}
	
	/**
	 * 发表运动圈动态
	 * @return
	 */
	@Action(value="addtrndynamic",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addTrnDynamic(){
		map = new HashMap<String, Object>();
		if (trnDynamicContent.getIdongId() > 0) {
			try {
				trnDynamicContentBiz.addTrnDynamic(trnDynamicContent);
				// 发表成功
				map.put("code", 1);
				map.put("result", "发表成功");
				return "succ";
			} catch (Exception e) {
				map.put("code", 0); // 发表失败
				map.put("msg", "系统异常");
				e.printStackTrace();
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失"); // 发表失败
			return "succ";
		}
	}
	
	/**
	 * 朋友圈背景图片修改
	 * @return
	 */
	@Action(value="editbackimage",results={@Result(name="succ",type="json",params={"root","map"})})
	public String editDynamicBackImage(){
		map = new HashMap<String, Object>();
		try {
			String backUrl = trnDynamicContent.getBackUrl();
			int idongId = trnDynamicContent.getIdongId();
			if(!"".equals(backUrl) && backUrl != null && idongId > 0){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("backUrl", backUrl);
				paraMap.put("idongId", idongId);
				trnDynamicContentBiz.editDynamicBackImage(paraMap);
				map.put("code", 1);
				map.put("result", "修改成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 运动圈举报
	 * @return
	 */
	@Action(value="reportdynamic",results={@Result(name="succ",type="json",params={"root","map"})})
	public String reportDynamic(){
		map = new HashMap<String, Object>();
		int dynamicId = trnDynamicContent.getDynamicId();
		int idongId = trnDynamicContent.getIdongId();
		if (dynamicId > 0 && idongId > 0 && ids > 0) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("dynamicId", dynamicId);
			paraMap.put("idongId", idongId);
			paraMap.put("rIdongId", ids);
			paraMap.put("addReason", addReason);
			try {
				trnDynamicContentBiz.addReportDynamic(paraMap);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
		map.put("code", 1);
		map.put("result", "举报成功");
		return "succ";
	}
}